public class Select_Test {

public Course_participant__c student{get; set;}
public String test{get; set;}
public String user{get; set;}
public String reason{get; set;}
public String q{get; set;}
public Double score {get; set;}
public Integer qGood{get; set;}
public Integer temp{get; set;}
public List<Test_Question__c> questions{get;set;}
public List<Integer> numbers{get;set;}
public Test_Question__c question{get;set;}
public Test__c testSelected{get;set;}
public Integer answer{get; set;}
public Integer qNumber{get; set;}
public boolean lastQ{get; set;}
public boolean lastQ2{get; set;}
public boolean status{get; set;}
public boolean canDo{get; set;}
public Double  failed{get; set;}
public Double  passed{get; set;}

public List<SelectOption> getAnswers() {

        question = questions.get(numbers.get((qNumber-1)));
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('1',question.Answer1__c)); 
        options.add(new SelectOption('2',question.Answer2__c)); 
        options.add(new SelectOption('3',question.Answer3__c)); 
        options.add(new SelectOption('4',question.Answer4__c)); 
        return options; 
    }
   public Select_Test() {
         lastQ = false;
         canDo = true;
         lastQ2 = true;
         qNumber = 1;
         qGood = 0;
         score = 0;
         user = UserInfo.getUserId();   
         
    }       

public List<SelectOption> gettestnames()
{
  List<Course_participant__c> courses = [SELECT Course__c FROM Course_participant__c WHERE Course_participant__c = :user AND Exam_Approved__c = FALSE];
  List<SelectOption> options = new List<SelectOption>();
  List<Test__c> testlist = new List<Test__c>();
  options.add(new SelectOption('--None--','--None--'));
  if (courses.size()>0){
  for (Course_participant__c course : courses){
      testlist = [Select Id, Name FROM Test__c WHERE Course__c = :course.Course__c ];
      for (Integer j=0;j<testlist.size();j++)
      {
          options.add(new SelectOption(testlist[j].Id,testlist[j].Name));
      }
  
    } 
  }
  return options;
}

public PageReference step1() {
      testSelected = [SELECT Name,  Number_of_Tries__c, Course__c, Type_of_Test__c, Passing_Score__c, Number_of_Questions__c, Maximun_Score__c, Points_per_Question__c, Times_Approved__c, Failed_times__c FROM Test__c WHERE Id = :test];
      questions = [SELECT Id, Name, Question__c, Answer1__c, Answer2__c, Answer3__c, Answer4__c, Correct_Answer__c FROM Test_Question__c WHERE Question_for_the_Test__c = :test ];
      student = [SELECT Id, Number_Tried__c, Exam_Approved__c, Score__c FROM Course_participant__c WHERE (Course__c = :testSelected.Course__c) AND (Course_participant__c = :user) ];
      if (student.Number_Tried__c < testSelected.Number_of_Tries__c ){ 
      canDo = true;
      } else {
      canDo = false;
      reason = 'You have reached your maximun number of tries, please contact your manager.';
      }
      if (student.Exam_Approved__c == true){
      canDo = false;
      reason = 'You have already passed this test';
      }
      numbers = new List<Integer>();       
         Set<Integer> rand = new Set<Integer>();
         while(rand.size() < testSelected.Number_of_Questions__c ) {
         temp = questions.size();
       
         Decimal rand2 = math.floor((Math.random()*temp));
         rand.add(rand2.intValue());
         }
         for (Integer x : rand) {
         numbers.add(x);
         }
      question = questions.get(numbers.get((qNumber-1)));
      q = question.Question__c;
      if (canDo == true){
      student.Number_Tried__c = student.Number_Tried__c + 1;
      update student; 
      return Page.test;
      } else {
      return Page.No_test;
      }
   }
   
public PageReference finalScore() {
      passed = testSelected.Times_Approved__c;
      failed = testSelected.Failed_times__c;
      if (answer == question.Correct_Answer__c){
      qGood = qGood + 1;
      score =qGood * testSelected.Points_per_Question__c;
          if (score >= testSelected.Passing_Score__c){
          status = true;
          }      
      }
       if (score >= testSelected.Passing_Score__c){         
          passed ++;
          student.Exam_Approved__c = true;
          } else {          
          failed ++;
          student.Exam_Approved__c = false;
      }
      student.score__c = score;
      testSelected.Times_Approved__c = passed;
      testSelected.Failed_times__c = failed;
      update testSelected;
      update student; 
      return Page.score;
}
public PageReference qNext() {  
      question = questions.get(numbers.get((qNumber-1)));  
      q = question.Question__c;
      if (answer == question.Correct_Answer__c){
      qGood = qGood + 1;
      score =qGood * testSelected.Points_per_Question__c;
          if (score >= testSelected.Passing_Score__c){
          status = true;
          }
      }
      
      if (qNumber < testSelected.Number_of_Questions__c){ 
      
      qNumber=qNumber + 1;      
      question = questions.get(numbers.get(qNumber-1));
      q = question.Question__c;
          if (qNumber == testSelected.Number_of_Questions__c){
              lastQ = true;
              lastQ2 = false;
          }
      }
      answer = null; 
      return null;
   }  
}